package main

import "strings"

type Bitset struct {
	size     int
	zero     int
	one      int
	resverse int
	arr      []int
}

func Constructor(size int) Bitset {
	arr := make([]int, (size+31)/32)
	b := &Bitset{size, size, 0, 0, arr}
	return *b
}

func (this *Bitset) Fix(idx int) {
	index := idx / 32
	bit := idx % 32
	if this.resverse == 0 {
		if (this.arr[index] & (1 << bit)) == 0 {
			this.zero--
			this.one++
			this.arr[index] = this.arr[index] | (1 << bit)
		}
	} else {
		if (this.arr[index] & (1 << bit)) != 0 {
			this.zero--
			this.one++
			this.arr[index] = this.arr[index] ^ (1 << bit)
		}

	}
}

func (this *Bitset) Unfix(idx int) {
	index := idx / 32
	bit := idx % 32
	if this.resverse == 0 {
		if (this.arr[index] & (1 << bit)) != 0 {
			this.zero++
			this.one--
			this.arr[index] = this.arr[index] ^ (1 << bit)
		}
	} else {
		if (this.arr[index] & (1 << bit)) == 0 {
			this.zero++
			this.one--
			this.arr[index] = this.arr[index] | (1 << bit)
		}

	}

}

func (this *Bitset) Flip() {
	this.resverse = this.resverse ^ 1
	temp := this.one
	this.one = this.zero
	this.zero = temp

}

func (this *Bitset) All() bool {
	return this.one == this.size
}

func (this *Bitset) One() bool {
	return this.one > 0
}

func (this *Bitset) Count() int {
	return this.one
}

func (this *Bitset) ToString() string {
	sb := strings.Builder{}
	k := 0
	for i := 0; i < len(this.arr); i++ {
		n := this.arr[i]
		for j := 0; j < 32 && k < this.size; j++ {
			k++
			status := (n >> j) & 1
			if this.resverse == 1 {
				status = status ^ 1
			}
			if status == 1 {
				sb.WriteString("1")
			} else {
				sb.WriteString("0")
			}

		}
	}
	return sb.String()
}

func main() {
	//fmt.Println(get_area(1, 2, [][]int{{0, 0, 2, 2}, {1, 0, 2, 3}, {1, 0, 3, 1}}))
	//2222
}

//0 1 2 0
//0 1 2 3
//1 2 3 0
